<template>
    <div id="main" onbeforeunload="checkLeave()" v-if='canUse'>
        <div style="height:40px;line-height:40px;background: #eaeaea;">
            <i class="iconfont icon-close" @click='fnclose()'
                style="font-size: 2rem;color: rgb(84, 84, 84);padding-left: 17px;cursor:pointer;"></i>

            <div @click='fnSaveBlog()'
                style="cursor:pointer;height: 100%;float: right;width: 6rem;text-align: center;color: white;background:linear-gradient(45deg, #9000ff, #5e00ff)">
                发布</div>

            <div @click='putPhone()' :style="isTop=='关'?'color: white;background: rgb(66, 154, 64);':''"
                style="cursor:pointer;height: 100%;float: right;width: 7rem;text-align: center;color: #474844;background: #dfe0dc; user-select:none;">
                APP激活</div>
        </div>

        <el-form ref="form" label-width="80px"
            style="    background: white;box-shadow: 0 0 38px 1px #d8d8d8;padding: 48px 0px;">
            <el-form-item label="*标题">
                <el-input placeholder="请输入标题内容" v-model="title">
                </el-input>
            </el-form-item>

            <el-form-item label="*链接" >
                <el-input placeholder="请输入下载链接" v-model="panUrl">
                </el-input>
            </el-form-item>

            <el-form-item label="*密码" >
                <el-input placeholder="请输入提取码,没有可填空" v-model="panPass">
                </el-input>
            </el-form-item>

            <el-form-item label="*封面">
                <el-upload class="upload-demo" :action="uploadUrl" :on-change="handleChange" :file-list="fileList"
                    style="" :on-success='uploadSuccess' :on-progress='uploadProgress'>
                    <el-button  size="small" type="primary">点击上传</el-button>
                </el-upload>
            </el-form-item>

            <el-form-item label="*分类">
                <el-select placeholder="请选择分类" v-model="blogClass">
                    <el-option :label="blogclass" :value="blogclass" v-for='blogclass in blogClassList'
                        :key="blogclass.id">

                    </el-option>
                </el-select>
            </el-form-item>

        </el-form>

    </div>
</template>


<script>
import { mapGetters, mapActions } from "vuex";
import auth from "../../api/auth";
import blog from "../../api/blog";
import Header from "../../components/Header/Header";
import Footer from "../../components/Footer/Footer";
// import Auth from "../../helpers/loginAnimate.js";
import lottieSave from '../../components/lottie/lottie-save'
import axios from 'axios'
import nsgame from '../../api/nsgame'
export default {
    name: "index",
    components: {
        Header,
        Footer,
        lottieSave
    },
    data() {
        return {
            title: '',
            markdownValue: '123',
            blogClass: '',
            blogClassList: ['xci中文', 'xci英日', 'nsp中文', 'nsp英日','nsz英日' ,'DLC/补丁', '教程'],
            interval: '',
            bgImg: '',
            attach: '',
            fileList: [],
            panUrl: '',
            panPass: '',
            uploadUrl: blog.upImgAvatar(),//请求的地址
            progress: 0,
            showProgress: false,
            upFileUrl: '',//请求成功的地址
            isIndex: '开',
            isTop: '关',
            canUse: false,
        };
    },
    created() {
        console.log('created')

    },
    mounted() {
        if (localStorage.getItem('canUse')) {
            this.canUse = true
        } else {
            this.$prompt('请输入密码', '权限认证', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
            }).then(({ value }) => {
                // this.password = this.$md5(value)
                if (value == 'nsGameControl') {
                    this.canUse = true
                    localStorage.setItem('canUse', true)
                }
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '取消更改'
                });
            });
        }

    },
    methods: {
        ...mapActions(["login", "checkLogin", "logout", "getPermissions"]),
        clearUpfile() {
            this.upFileUrl = ''
        },
        putPhone() {
            this.$prompt('请输入手机号', '设备激活', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
            }).then(({ value }) => {
                // this.password = this.$md5(value)
                nsgame.putPhone(value).then(res => {
                    console.log('激活成功')
                    this.$message.success(res.data)
                }).catch(res => {
                    this.$message.error('激活失败')
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '取消更改'
                });
            });
        },
        uploadSuccess(e) {//上传附件
            console.log(e)
            // this.showProgress = false
            if (e.success) {
                this.upFileUrl = e.data
            } else {
                this.$message.error('上传失败')
            }
        },
        uploadProgress(e) {
            this.showProgress = true
            this.progress = e.percent
        },
        handleChange(file, fileList) {
            // this.fileList = fileList.slice(-3);
        },
        beforeAvatarUpload(file) {
            const isJPG = file.type === 'image/jpeg';
            const isPNG = file.type === 'image/png';
            const isLt2M = file.size / 1024 / 1024 < 4;

            if (!isJPG && !isPNG) {
                this.$message.error('上传头像图片只能是 JPG/PNG 格式!');
            }
            if (!isLt2M) {
                this.$message.error('上传图片大小不能超过 4MB!');
            }
            return isJPG && isLt2M && isPNG;
        },
        fnclose() {
            window.close();
        },
        async fnSaveBlog() {
            console.log(this.title, this.panUrl, this.panPass, this.uploadUrl, this.blogClass)
            try {
                if (this.title == '' || this.panUrl == '' || this.panPass == '' || this.uploadUrl == '' || this.blogClass == '') return this.$message({ message: '输入内容不可为空', type: 'warning' })
                let isTop = this.isTop == '开' ? 1 : 0
                let isIndex = this.isIndex == '开' ? 1 : 0
                let res = await nsgame.addGame({ title: this.title, img: this.upFileUrl, url: this.panUrl, password: this.panPass, class: this.blogClass })
                console.log(res)
                if (res.success) {
                    this.$message({ message: '保存成功', type: 'success' })
                    //清空数据
                    //clearInterval(this.interval)
                    this.title = ''
                    this.upFileUrl = ''
                    this.panUrl = ''
                    this.panPass = ''
                    this.blogClass = ''
                }
            } catch (err) {
                console.log(err)
                this.$message.error({ message: err.errorMsg })
            }
        },
        //初始化数据
        initData() {
            // if (localStorage.getItem('blogSave')) {
            //     let blogSave2 = JSON.parse(localStorage.getItem('blogSave'))
            //     this.title = blogSave2.title
            //     this.blogClass = blogSave2.blogClass
            //     this.markdownValue = blogSave2.body
            //     this.upFileUrl = blogSave2.upFileUrl
            //     this.bgImg = blogSave2.bgImg
            // }
        },
        autoSave() {
            // this.interval = setInterval(() => {
            //     let blogSave = {}
            //     blogSave.title = this.title //自动保存标题
            //     blogSave.blogClass = this.blogClass//自动保存分类
            //     blogSave.body = this.markdownValue
            //     blogSave.upFileUrl = this.upFileUrl
            //     blogSave.bgImg = this.bgImg
            //     localStorage.setItem('blogSave', JSON.stringify(blogSave))//把用户信息存到localStorage
            // }, 10000)
        },
        checkLeave() {
            event.returnValue = "确定离开当前页面吗？";
        },
        // 绑定@imgAdd event

        $imgAdd(pos, $file) {
            //上传图片接口
            console.log($file)
            var formdata = new FormData();
            formdata.append('file', $file);
            blog.upImg(formdata).then(res => {
                if (res.success) {
                    this.$message({ message: '图片上传成功', type: 'success' })
                    if (!this.bgImg) {
                        this.bgImg = res.data
                    }
                    this.$refs.md.$img2Url(pos, res.data);
                } else {
                    this.$message.error({ message: '上传图片失败' })
                }
            })
        },
    },
    computed: {
        ...mapGetters(["isLogin", "user"])
    }
};
</script>

<style lang="less" scoped>
@import "../../assets/css/auth.css";

::-webkit-scrollbar-thumb {
    background: white !important;
}
::-webkit-scrollbar {
    width: 6px;
    height: 6px;
    background-color: white !important;
}
.upLoad {
    height: 40px;
    display: inline-block;
    background: #696969;
    width: 70px;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 13px;
    color: #ffffff;
}
.progress {
    height: 40px;
    display: inline-block;
    width: calc(100% - 70px);
    background: #5252523d;
    position: absolute;
    top: 0;
    margin-left: 70px;
    display: flex;
    justify-content: center;
    align-items: center;
}
.upFileUrl {
    height: 40px;
    display: inline-block;
    width: calc(90% - 70px);
    position: absolute;
    top: 0;
    margin-left: 70px;
    display: flex;
    align-items: center;
    padding-left: 17px;
}
a {
    position: relative;
    display: inline-block;
    padding: 1.2em 2em;
    text-decoration: none;
    text-align: center;
    cursor: pointer;
    user-select: none;
    color: white;

    &::before {
        content: "";
        position: absolute;
        top: 0;
        left: 0;
        bottom: 0;
        right: 0;
        background: linear-gradient(135deg, #6e8efb, #a777e3);
        border-radius: 4px;
        transition: box-shadow 0.5s ease, transform 0.2s ease;
        will-change: transform;
        box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
        transform: translateY(var(--ty, 0)) rotateX(var(--rx, 0))
            rotateY(var(--ry, 0)) translateZ(var(--tz, -12px));
    }

    &:hover::before {
        box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
    }

    &::after {
        position: relative;
        display: inline-block;
        content: attr(data-title);
        transition: transform 0.2s ease;
        font-weight: bold;
        letter-spacing: 0.01em;
        will-change: transform;
        transform: translateY(var(--ty, 0)) rotateX(var(--rx, 0))
            rotateY(var(--ry, 0));
    }
}

#main {
    height: 100vh;
}
.v-note-op {
    border: 1px solid #e6e6e6 !important;
    box-shadow: none !important;
}
.btn {
    position: fixed;
    top: 87%;
    right: 3%;
    z-index: 9999;
    height: 50px;
    width: 50px;
    background: #f97575;
    border-radius: 50%;
    color: white;
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
    transition: all 0.3s;
    &:hover {
        background: #ff6262;
        box-shadow: 0 0 6px 1px #ff8888;
        transition: all 0.3s;
    }
}
.markdown-body img {
    max-height: 40rem;
}
</style>

